/**
 * 状态码列表。请使用get方法获取相应的状态信息。<br>
 * 开发人员可根据状态码信息处理相应的逻辑。<br>
 *
 * @module ChiVoxSDK/lib
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import __code from './code.json';

/**
 * StatusCode类。<br/><br/>
 *
 * 根据状态代码获得状态信息。<br>
 * 返回的对象至少包含这几个字段：id, description, feedback
 * @param {string} code
 * @params {string} language 语言，可选值为"en","cn"。默认为"en"
 * @return {object}
 */
class StatusCode {
    /**
     * 根据状态代码获得状态信息。<br>
     * 返回的对象至少包含这几个字段：id, description, feedback
     *
     * @param {string} code
     * @params {string} language 语言，可选值为"en","cn"。默认为"en"
     * @return {object}
     */
    static get(code, language) {
        var r = { id: code, description: "未知错误。", state: "", feedback: { en: "Please try again.", cn: "请重试。" } };

        if (code == "NO_DATA") {
            r = { id: code, state: "NO_DATA", description: "指定之间内获取录音ID失败", feedback: { en: "Recording failed, please try again.", cn: "录音失败，请重试。" } };
        } else if (code == "TIMEOUT") {
            r = { d: code, state: "TIMEOUT", description: "指定之间内评分失败", feedback: { en: "Core request timeout, please try again.", cn: "评分超时，请重试。" } };
        } else {
            if (typeof __code[code] !== "undefined") {
                var result = __code[code];
                r.id = result.id;
                r.state = result.state || "";
                r.description = result.description;
                r.feedback = result.feedback;
            } else {
                var s = code.toString();
                var i = s.charAt(0);
                var iCode = parseFloat(code);
                if (iCode >= 0 && iCode <= 39999) {
                    r = __code["default2"];
                } else if (iCode >= 40000 & iCode <= 69999) {
                    r = __code["default" + i];
                }
            }
        }
        if (language == "cn") {
            r.feedback = (r.feedback && r.feedback.cn) || r.feedback;
        } else {
            r.feedback = (r.feedback && r.feedback.en) || r.feedback;
        }
        if (r.id && r.feedback) {
            r.feedback = r.id + ": " + r.feedback;
        }
        return r;
    }

    static trySet(code, description) {
        if (typeof __code[code] === "undefined") {
            __code[code] = { id: "error", description: description,
                feedback: { en: description, cn: description }
            };
        }
    }
}

export default StatusCode
